package com.example.bdmanagerapplication

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener
import com.baidu.mapapi.search.poi.PoiCitySearchOption
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.baidu.mapapi.search.poi.PoiDetailSearchResult
import com.baidu.mapapi.search.poi.PoiIndoorResult
import com.baidu.mapapi.search.poi.PoiResult
import com.baidu.mapapi.search.poi.PoiSearch
import com.example.bdmanagerapplication.MapUtil.launchAMap


import com.example.bdmanagerapplication.MyUtils.setOnKeySearchListener
import com.example.bdmanagerapplication.databinding.ActivitySecondBinding
import com.google.gson.Gson
import org.jetbrains.anko.startActivity


class SecondActivity : AppCompatActivity(),CustomAdapter.CallBack {
    private lateinit var mBinding: ActivitySecondBinding
    private val mList = ArrayList<MyPoiInfo>()
    private val poiDao by lazy { AppDatabase.getDatabase(this).poiInfoDao() }
    private val mViewModel by lazy { ViewModelProvider(this)[PoiInfoViewModel::class.java] }
    private val adapter=CustomAdapter(mList,this)
    private var listener: OnGetPoiSearchResultListener = object : OnGetPoiSearchResultListener {
        override fun onGetPoiResult(p0: PoiResult?) {
            p0?.allPoi?.let {list->
                mList.clear()
                list.forEach {
                    val gson = Gson()
                    Log.d("测试数据",it.toString())
                    val myPoiInfo = gson.fromJson(gson.toJson(it), MyPoiInfo::class.java)
                    mList.add(myPoiInfo)
                }
                mViewModel.getChangeList(poiDao,mList)
            }
        }

        override fun onGetPoiDetailResult(p0: PoiDetailResult?) {

        }

        override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {

        }

        override fun onGetPoiIndoorResult(p0: PoiIndoorResult?) {

        }


    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding=ActivitySecondBinding.inflate(layoutInflater)
        enableEdgeToEdge()
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val mPoiSearch = PoiSearch.newInstance()
        mPoiSearch.setOnGetPoiSearchResultListener(listener)
        mBinding.mNearRV.layoutManager=LinearLayoutManager(this)
        mBinding.mNearRV.adapter=adapter
        mBinding.mSearchET.setOnKeySearchListener {
            val key=mBinding.mSearchET.text.toString()
            mPoiSearch.searchInCity(
                PoiCitySearchOption()
                    .city("无锡") //必填
                    .keyword(key) //必填
                    .pageNum(0)
            )
        }
        mViewModel.isPoiExistsResult.observe(this){
            mList.clear()
            mList.addAll(it)
            adapter.notifyDataSetChanged()
        }
    }

    override fun addItemClick(data: MyPoiInfo, position: Int) {
        data.isClient=1
        adapter.notifyItemChanged(position)
        var poiEntity=PoiEntity()
        poiEntity.uid=data.uid
        poiEntity.name=data.name
        poiEntity.latitude=data.location.latitude
        poiEntity.longitude=data.location.longitude
        poiEntity.area=data.area
        mViewModel.addPoi(poiDao,poiEntity)
    }

    override fun itemNearClick(data: MyPoiInfo, position: Int) {
        val poiEntity=PoiEntity()
        poiEntity.uid=data.uid
        poiEntity.name=data.name
        poiEntity.latitude=data.location.latitude
        poiEntity.longitude=data.location.longitude
        startActivity<NearActivity>("entity" to poiEntity)
    }
    override fun navigateClick(data: MyPoiInfo, position: Int) {
        launchAMap(this,data.location.latitude,data.location.longitude,data.name)
    }
}